package com.joysuch.wwyt.dynamicdata.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.zlg.common.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.joysuch.wwyt.common.constant.Constant;
import com.joysuch.wwyt.core.enums.DeleteFlag;
import com.joysuch.wwyt.core.enums.Status;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.dynamicdata.entity.BaseDynamicBizMapping;
import com.joysuch.wwyt.dynamicdata.entity.BaseDynamicFieldConfig;
import com.joysuch.wwyt.dynamicdata.entity.DropDownSelectBean;
import com.joysuch.wwyt.dynamicdata.enums.ComponentTypeEnum;
import com.joysuch.wwyt.dynamicdata.mapper.BaseDynamicBizMapper;
import com.joysuch.wwyt.dynamicdata.mapper.BaseDynamicFieldConfigMapper;
import com.joysuch.wwyt.dynamicdata.service.BaseDynamicFieldConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joysuch.wwyt.dynamicdata.entity.vo.BaseDynamicBizVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jwk
 * @since 2021-11-26
 */
@Service
public class BaseDynamicFieldConfigServiceImpl extends ServiceImpl<BaseDynamicFieldConfigMapper, BaseDynamicFieldConfig> implements BaseDynamicFieldConfigService {

    @Autowired
    private BaseDynamicBizMapper baseDynamicBizMapper;

    @Autowired
    private BaseUserDao baseUserDao;

    @Override
    public List<BaseDynamicFieldConfig> findByBizMappingId(Long bizMappingId) {
        LambdaQueryWrapper<BaseDynamicFieldConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseDynamicFieldConfig::getDeleteFlag, DeleteFlag.EFFECTIVE.getCode());
        wrapper.eq(BaseDynamicFieldConfig::getBizMappingId, bizMappingId);
        wrapper.eq(BaseDynamicFieldConfig::getTenentId, Context.getCurrentTenantId());
        wrapper.likeRight(BaseDynamicFieldConfig::getOrgCode, Context.getCompanyCode());
        return super.list(wrapper);
    }

    @Override
    public List<BaseDynamicBizMapping> findAllBizMapping() {
        LambdaQueryWrapper<BaseDynamicBizMapping> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseDynamicBizMapping::getDeleteFlag, DeleteFlag.EFFECTIVE.getCode());
        wrapper.eq(BaseDynamicBizMapping::getTenentId, Context.getCurrentTenantId());
        wrapper.likeRight(BaseDynamicBizMapping::getOrgCode, Context.getCompanyCode());
        return baseDynamicBizMapper.selectList(wrapper);
    }

    @Override
    public List<BaseDynamicFieldConfig> findByBizType(String bizType) {
        List<BaseDynamicFieldConfig> configList = new ArrayList<>();
        LambdaQueryWrapper<BaseDynamicBizMapping> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseDynamicBizMapping::getBizType, bizType);
        wrapper.eq(BaseDynamicBizMapping::getDeleteFlag, DeleteFlag.EFFECTIVE.getCode());
        wrapper.eq(BaseDynamicBizMapping::getTenentId, Context.getCurrentTenantId());
        wrapper.likeRight(BaseDynamicBizMapping::getOrgCode, Context.getCompanyCode());
        BaseDynamicBizMapping dynamicBiz = baseDynamicBizMapper.selectOne(wrapper);
        if (dynamicBiz != null) {
            configList = findByBizMappingId(dynamicBiz.getId());
        }
        return configList;
    }

    @Override
    public List<BaseDynamicBizVO> findSummaryList() {
        List<BaseDynamicBizVO> voList = new ArrayList<>();
        List<BaseDynamicBizMapping> bizList = findAllBizMapping();
        bizList.forEach(each -> {
            BaseDynamicBizVO bizVO = new BaseDynamicBizVO();
            bizVO.setBizDesc(each.getBizDesc());
            bizVO.setBizType(each.getBizType());
            bizVO.setUpdateTime(each.getUpdateTime());
            bizVO.setBizMappingId(each.getId());
            bizVO.setCreatorName(baseUserDao.getRealNameById(each.getCreateBy()));
            List<BaseDynamicFieldConfig> configList = findByBizType(each.getBizType());
            bizVO.setFieldNum(configList.size());
            String fieldDesc = configList.stream().map(BaseDynamicFieldConfig::getFieldDesc).collect(Collectors.joining(Constant.SPLIT_COMMA));
            bizVO.setFieldDesc(fieldDesc);
            voList.add(bizVO);
        });
        return voList;
    }

    @Override
    public List<DropDownSelectBean> componentTypeList() {
        Map<String, String> kv = ComponentTypeEnum.getKV();
        List<DropDownSelectBean> listBean = new ArrayList<>();
        kv.forEach((k, v) -> {
            DropDownSelectBean bean = new DropDownSelectBean();
            bean.setLabel(k);
            bean.setValue(v);
            listBean.add(bean);
        });
        return listBean;
    }

    @Override
    public boolean batchDelete(List<Long> configIds) {
        List<BaseDynamicFieldConfig> configList = new ArrayList<>();
        configIds.forEach(each -> {
            BaseDynamicFieldConfig point = new BaseDynamicFieldConfig();
            point.setDeleteFlag(DeleteFlag.DELETED.getCode());
            point.setId(each);
            configList.add(point);
        });
        return super.saveOrUpdateBatch(configList);
    }

    @Override
    public void saveConfigOrUpdateBatch(List<BaseDynamicFieldConfig> dynamicFieldConfigs) throws ApiBusinessException {
        Set<String> fieldNames = new HashSet<>();
        if (CollectionUtils.isNotEmpty(dynamicFieldConfigs)) {
            for (BaseDynamicFieldConfig each : dynamicFieldConfigs) {
                if (fieldNames.contains(each.getFieldDesc())) {
                    throw new ApiBusinessException(Status.DYNAMIC_FIELD_LABEL_EXIST);
                }
                if (StringUtils.isBlank(each.getFieldName())) {
                    each.setFieldName(IdUtil.simpleUUID());
                }
                fieldNames.add(each.getFieldDesc());
            }
            super.saveOrUpdateBatch(dynamicFieldConfigs);
        }
    }


}
